import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'home_page.dart';
import 'category_page.dart';
import 'cart_page.dart';
import 'member_page.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../provide/current_index_provider.dart';

class IndexPage extends StatelessWidget {
  final List<BottomNavigationBarItem> bottomTabs = [
    BottomNavigationBarItem(icon: Icon(CupertinoIcons.home), title: Text('首页')),
    BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.search), title: Text('分类')),
    BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.shopping_cart), title: Text('购物车')),
    BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.profile_circled), title: Text('我的')),
  ];

  final List<Widget> tabBodies = [
    HomePage(),
    CategoryPage(),
    CartPage(),
    MemberPage()
  ];

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
//    print('屏幕尺寸:' + size.toString());
    var defWidth = 750.0;
    var defHeight = 1334.0;

    if ((size.width - size.height).abs() <= 150) {
      defWidth = size.width;
      defHeight = size.height;
    }

    ScreenUtil.init(context, width: defWidth, height: defHeight);

    return Consumer<CurrentIndexProvider>(builder: (context, val, child) {
      return Scaffold(
        backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
        bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          currentIndex: val.currentIndex,
          items: bottomTabs,
          onTap: (index) {
            val.changeIndex(index);
          },
        ),
        body: IndexedStack(index: val.currentIndex, children: tabBodies),
      );
    });
  }
}
